//
//  BusinessTableViewController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/5/30.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BusinessTableViewController.h"
#import "BusinessViewCell.h"
#import "BusinessDetailsController.h"

#import "BusinessModel.h"



@interface BusinessTableViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    TopPullRefreshAnaimation *gifHeader;
    MBProgressHUD *progressHUD;
    NSUInteger refreshIndex;
}

@property(nonatomic,strong)UITableView *businessView;

@property(nonatomic,strong)NSMutableArray *businessArray;


@end

@implementation BusinessTableViewController

static NSString *const businessIdentifier = @"BusinessTableViewCell";

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    
    //数据加载
    [self BusinessDetailsLoadData];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [progressHUD hide:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTableView];
    //上拉刷新
    [self addPullUpdateData];
    
    
    //
    refreshIndex = 1;
}

- (void)addTableView
{
    UITableView *businessView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.businessView = businessView;
    [self.view addSubview:businessView];
    self.businessView.rowHeight = HEIGHT/6;
    
    businessView.delegate = self;
    businessView.dataSource = self;
    
}

- (void)addPullUpdateData
{
    self.businessView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(BusinessDetailsLoadMoreData)];
    
}

- (void)BusinessDetailsLoadMoreData
{
    refreshIndex ++;
    [self BusinessDetailsLoadData];
    [self.businessView.mj_footer endRefreshing];
}

- (void)BusinessDetailsLoadData {
    
    NSString *city = @"丽水";
    NSString *P = [NSString stringWithFormat:@"%ld",refreshIndex];
    
    NSDictionary *params = @{@"city":[NSString stringWithFormat:@"%@", city],@"p":[NSString stringWithFormat:@"%@",P]};
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Business/lists" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        NSArray *arr = object[@"data"];
        if ([[object objectForKey:@"code"] isEqual:@(1)]) {
            for (NSDictionary *dict in arr) {
                BusinessModel *businessModel = [[BusinessModel alloc] initWithDictionary:dict error:nil];
         
                if (businessModel) {
                    [self.businessArray addObject:businessModel];
                    //菊花隐藏
                    [progressHUD hide:YES];
                } else{
                //
                [self.businessView.mj_footer endRefreshingWithNoMoreData];
            }
                
        }
            
        }else{
            //
            [self.businessView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.businessView reloadData];
        
    }];
    
    
}




#pragma mark>>>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.businessArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    BusinessViewCell *cell = [tableView dequeueReusableCellWithIdentifier:businessIdentifier];
    
    if (cell==nil) {
        
        cell = [[BusinessViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:businessIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    BusinessModel *businessModel = self.businessArray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"http://www.ylgw8.com/Uploads/sjpic/s_%@",businessModel.sdpic];
    [cell.shopsImg sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"businessNopict.jpg"]];
    cell.shopsNameLabel.text = businessModel.title;
    cell.shopsDetailsLabel.text = businessModel.zycp;
    [cell.shopsAddress setTitle:businessModel.county forState:UIControlStateNormal];
    
    
    return cell;
}


#pragma mark>>>>>>>>>>>>代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BusinessDetailsController *detailVC = [[BusinessDetailsController alloc] init];
    BusinessModel *businessModel = self.businessArray[indexPath.row];
    detailVC.bussinessId = businessModel.replaceID;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:detailVC];
}

- (NSMutableArray *)businessArray {
    if (!_businessArray) {
        _businessArray = [NSMutableArray array];
    }
    
    return _businessArray;
}

@end
